package com.syh.bishe.service.impl;

import com.syh.bishe.mapper.LogMapper;
import com.syh.bishe.pojo.CarbonCredits;
import com.syh.bishe.mapper.CarbonCreditsMapper;
import com.syh.bishe.pojo.Log;
import com.syh.bishe.pojo.Transactions;
import com.syh.bishe.service.ICarbonCreditsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syh.bishe.service.ILogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author syh
 * @since 2024-07-02
 */
@Service
@RequiredArgsConstructor
public class CarbonCreditsServiceImpl extends ServiceImpl<CarbonCreditsMapper, CarbonCredits> implements ICarbonCreditsService {
    private final CarbonCreditsMapper carbonCreditsMapper;
    private final LogMapper logMapper;
    @Override
    public BigDecimal getCreditAmount(Integer companyId) {
        if (carbonCreditsMapper.getCreditAmount(companyId) != null) {
            return carbonCreditsMapper.getCreditAmount(companyId);
        }
        return new BigDecimal(0);
    }

    @Override
    public BigDecimal getAccount(Integer companyId) {
        if (carbonCreditsMapper.getAccount(companyId) != null) {
            return carbonCreditsMapper.getAccount(companyId);
        }
        return new BigDecimal(0);
    }

    @Override
    @Transactional
    public void updateAccount(CarbonCredits carbonCredits) {
        logMapper.insert(new Log().setInfo("公司ID:"+carbonCredits.getCompanyId()+"充值账户余额"+carbonCredits.getAccount()+"元").
                setDate(LocalDateTime.now()).setUserId(carbonCredits.getCompanyId()));
        carbonCreditsMapper.updateAccount(carbonCredits);
    }

    @Override
    public void updateCreditAmount(Integer companyId, BigDecimal emissionAmount) {
        carbonCreditsMapper.updateCreditAmount(companyId, emissionAmount);
    }

    @Override
    public void updateCreditAmountAdd(Integer companyId, BigDecimal emissionAmount) {
        carbonCreditsMapper.updateCreditAmountAdd(companyId, emissionAmount);
    }

    @Override
    public void updateAccount_a(Transactions transactions) {
        carbonCreditsMapper.updateAccount_a(transactions);
    }

    @Override
    public void updateAccount_s(Transactions transactions) {
        carbonCreditsMapper.updateAccount_s(transactions);
    }

    @Override
    public void updateAccount_add_buy(Transactions transactions) {
        carbonCreditsMapper.updateAccount_add_buy(transactions);
    }

    @Override
    public void updateAccount_add_sell(Transactions transactions) {
        carbonCreditsMapper.updateAccount_add_sell(transactions);
    }
}
